package ovm.management;

import ovm.entity.EntityCollectionPropertiesEntity;

import javax.persistence.Query;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Jokser
 * Date: 08.06.12
 * Time: 8:54
 * To change this template use File | Settings | File Templates.
 */
public class EntityCollectionManager extends AbstractManager {

    public static List<EntityCollectionProperty> getCollections (Class entityClass) {
        String className = entityClass.getSimpleName();
        
        Query query = em.createQuery("SELECT e FROM EntityCollectionPropertiesEntity e WHERE e.entityClass = :entityClass");
        query.setParameter("entityClass", className);

        List<EntityCollectionPropertiesEntity> list = query.getResultList();

        List<EntityCollectionProperty> result = new ArrayList<EntityCollectionProperty>();

        for (EntityCollectionPropertiesEntity property : list) {
            try {
                result.add(new EntityCollectionProperty(Class.forName("ovm.entity."+property.getReferenceClass()), property.getReferenceParameter()));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }

        return result;

    }

}
